<extend name="Base/common"/>
<block name="side">
    <div class="sidebar">
        <ul class="side-nav">
            <include file="menu"/>
        </ul>
    </div>
</block>
<block name="body">

    <div id="main-content">
        <div id="main" class="main" style="min-height: 380px;">
            <div class="row">
                <h1>经销商管理</h1>
                <div class="admin">
                    欢迎 <span style="color: red;">{:get_nickname()}</span> 登陆系统
                </div>
                <include file="Public/dashboardbn"/>
            </div>
            <div class="search-form">
                <form id="search" action="{:U('Index/search')}" method="post" >

                    <div class="sleft">
                        <span class="search-title">经销商代码</span>
                        <input id="username" class="search-input" type="text" placeholder="" value="{:I('username','')}" name="username">
                        <span class="search-title">城市</span>
                        <input id="city" class="search-input" type="text" placeholder="" value="{:I('city','')}" name="city">
                    </div>
                    <input type="hidden" name="controller" value="{$CONTROLLER_NAME}">
                    <input type="hidden" name="action" value="{$ACTION_NAME}">
                    <button id="searchSubmit" class="btn ajax-post" target-form="ids" type="submit">搜索</button>
                    <a class="btn ajax-post" target-form="ids"  style="float: right;" href="__SELF__">刷新</a>
                </form>
            </div>
            <div class="table-responsive">
                <table class="table table-hover table-striped">
                    <thead>
                        <tr>
                            <th>NO.</th>
                            <th>经销商名称</th>
                            <th>登陆密码</th>
                            <th>经销商代码</th>
                            <th>城市</th>
                            <th>总配额</th>
                            <th>已用配额</th>
                            <th>剩余配额</th>
                            <th>补贴金额</th>
                            <th>修改时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>

                        <volist name="list" id="info" key="key">
                            <!--<form action="{:U('Agent/dealermanage')}" method="post" >-->
                                <input style="display:none;" type="text" name="id" value="{$info.id}">
                                <tr class="record" data-id="{$key}" data-picture="{:get_file($info['statement'],'path')}">
                                    <p class="statement_picture" style="display:none;">{:get_file($info['statement'],'path')}</p>

                                    <td>{$key+10*(I('p',1)-1)}</td>
                                    <td>{:get_nickname($info['id'])}</td>
                                    <td>{$info.cleartext}</td>
                                    <td>{$info.dealernumber}</td>
                                    <td>{$info.city}</td>
                                    <td data-quota="{$info.id}" data-quota-old="{$info.quota}">{$info.quota}</td>
                                    <td data-used="{$info.id}">{$info.used}</td>
                                    <td data-remain="{$info.id}" data-remain-old="{:minus($info['quota'],$info['used'])}">
                                        {:minus($info['quota'],$info['used'])}
                                    </td>
                                    <td>
                                        <if condition="$info['price']">
                                            ￥{$info.price}
                                            <else/>
                                            --
                                        </if>
                                    </td>
                                    <td>{$info.reg_time|date='Y-m-d',###}</td>
                                    <td>
                                        <button class="change" style="cursor: pointer;" data-id="{$info.id}">修改配额</button>
                                    </td>
                                </tr>
                            <!--</form>-->

                        </volist>
                    </tbody>
                </table>
            </div>
            <div class="page">
                <div>
                    {$_page}
                </div>
            </div>
            <script>
                $(function(){
                    $("button.change").on('click',function(e){
                        var $this = $(this), $quota, $used, $remain,$quota_new, $quota_number, $quota_old, $input, $id, $callback;
                        
                        $id = $this.data("id");// 经销商ID
                        $quota = $("[data-quota='"+$id+"']");// 总配额元素
                        $used = $("[data-used='"+$id+"']")// 已用配额元素
                        $remain = $("[data-remain='"+$id+"']");// 剩余配额元素
                        
                        // 执行修改
                        if ( $this.text() == "确认修改" ) {
                            
                            // 获取表单元素
                            $input = $quota.children("input");
                            
                            // 获取新的总配额
                            $quota_new = parseInt($input.val());
                            
                            // 修改配额回调函数
                            $callback = function ($res){
                                if ( $res.status == 1 ) {
                                    
                                    // 修改总配额显示
                                    $quota.text($res.data.quota);
                                    
                                    // 修改已用配额显示
                                    $used.text($res.data.used);
                                    
                                    // 修改剩余配额显示
                                    $remain.text($res.data.quota - $res.data.used);
                                } else if( $res.status == 0){
                                    alert($res.msg);
                                    if ( $res.data ){
                                        // 修改总配额显示
                                        $quota.text($res.data.quota);

                                        // 修改已用配额显示
                                        $used.text($res.data.used);

                                        // 修改剩余配额显示
                                        $remain.text($res.data.quota - $res.data.used);
                                    }
                                } else {
                                    // 恢复剩余配额元素显示
                                    $quota_old = $quota.data('quota-old');
                                    $quota.text($quota_old);
                                }
                            };
                            // 调用api的addQuota操作
                            send_data({action: "addQuota", id: $id ,quota: $quota_new},"{:U('Agent/api')}", $callback);
                            
                            $this.text("修改配额");
                        } else {
                            $quota_number = parseInt($quota.text());// 当前显示的总配额
                            $quota.data("quota-old",$quota_number);
                            $input = $("<input name='quota' type='number' value='"+$quota_number+"' min='0' style='width: 45px;height:30px;border: 1px solid #3f496f;'>");
                            $quota.empty();
                            $quota.append($input);
                            $this.text("确认修改");
                        }
                        
                        
                        
                        e.preventDefault();
                    });
                    
                });
            </script>



        </div>
    </div>

</block>